How to use a lambda expression in Enumerable.Distinct()?
How to use a lambda expression in Enumerable.Distinct()?
27218-Aug-2023
Updated on 19-Aug-2023
Home / DeveloperSection / Forums / How to use a lambda expression in Enumerable.Distinct()?
How to use a lambda expression in Enumerable.Distinct()?
Aryan Kumar
19-Aug-2023Sure, I can help you with that.
To use a lambda expression in
Enumerable.Distinct()
, you need to pass the lambda expression as the predicate parameter to theDistinct()
method. The predicate parameter is a function that takes two elements of the sequence as its parameters and returns a boolean value. TheDistinct()
method will return a sequence that contains only the elements of the original sequence for which the predicate returnstrue
.Here is an example of how to use a lambda expression in
Enumerable.Distinct()
:C#
In this example, the lambda expression takes two integers as its parameters and returns a boolean value. The lambda expression returns
true
if the two integers are not equal, andfalse
if the two integers are equal. TheDistinct()
method will return a sequence that contains only the elements of the original sequence for which the predicate returnstrue
.